package cn.worldyao.demo.util;

import cn.worldyao.demo.entity.SysUser;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.io.*;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author yaozi
 */
public class Tools {

    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    /**
     * 生成短8位UUID
     *
     * @return
     */
    public static String UUID8() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }


    public static String MD5Pwd(SysUser user) {
        //可用户注册、登录 时使用
        // 加密算法MD5
        // salt盐 username + salt
        // 迭代次数 加密2次
        String md5Pwd = new SimpleHash("MD5", user.getPassword(), ByteSource.Util.bytes(user.getSalt()), 2).toHex();
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, passwordInDB, ByteSource.Util.bytes(salt), getName());
        return md5Pwd;
    }

    public static String read() throws IOException {
        String url = Tools.class.getClassLoader().getResource("other.config").toString();
        url = url.substring(6, url.length());
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(url));
        } catch (IOException error) {
            error.printStackTrace();
        }
        return properties.get("LAST_MEMBER_ID").toString();
    }


    public static String getNewMemberId() throws IOException {
        String lastMemberId = read();
        if (lastMemberId != null && !lastMemberId.isEmpty()) {
            int newEquipment = Integer.parseInt(lastMemberId) + 1;
            return String.format(String.valueOf(newEquipment));
        }
        return "ERROR";
    }

    public static String Salt8() {
        return RandomStringUtils.randomAlphanumeric(8);
    }

    public static void updateMembers(String equipmentNo) throws Exception {
        try {
            Properties properties = new Properties();
            String url = Tools.class.getClassLoader().getResource("other.config").toString();
            url = url.substring(6, url.length());
            properties.load(new FileInputStream(url));
            properties.setProperty("LAST_MEMBER_ID", equipmentNo);
            properties.store(new FileOutputStream(url), "更新会员ID");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException {
//        String equipmentNo = Tools.getNewMemberId();
//        System.out.println("卡号：" + equipmentNo);
        LocalDateTime localDateTime = LocalDateTime.now();
        int i = 10000;
        while (i > 1) {
            System.out.println(UUID8().toUpperCase());
            i--;
        }


//        Map map = new HashMap();
//        map.put("LAST_MEMBER_ID", equipmentNo);


    }


}
